//
//  YWKRegisterVC.m
//  YWKUser
//
//  Created by Admin on 16/3/29.
//  Copyright © 2016年 iCareTech. All rights reserved.
//

#import "YWKRegisterVC.h"
#import "YWKCustomTextField.h"
#import "YWKDrawLineView.h"
#import "YWKLable.h"
#import "YWKHelper.h"

#define labWidth       85
#define agreeServiceTermsText   @"点击注册，即代表你同意 云卫康用户协议"
#define YWKServiceTerms         @"云卫康用户协议"

#define kPhoneNumberTag              200
#define zCodeCountTime               60
#define kPlaceHolderColor [UIColor lightGrayColor]

@interface YWKRegisterVC ()<
UITextFieldDelegate,
UIScrollViewDelegate,
UIAlertViewDelegate,
UITextViewDelegate>
{
    NSInteger _count;
    float     _pwdTFWidth;        //输入密码的文本框长度
    float     _identifyTFWidth;   //输入验证码的文本框长度
    NSTimer   *_timer;
    BOOL      _isAccoutChecking;
    NSString  *_userN;           //账号名称
}

@property (nonatomic, strong) UIImageView *iCTLogoView;                 //C＋图
@property (nonatomic, strong) UIScrollView *rigisterScrollView;
@property (nonatomic, strong) UIView * scrollContentView;
//@property (nonatomic, strong) ICTOFClient *rigisterUser;
@property (nonatomic, strong) YWKCustomTextField *phoneNumberTextField; //手机号
@property (nonatomic, strong) YWKCustomTextField *identifyingCodeTF;    //验证码
@property (nonatomic, strong) YWKCustomTextField *pwdTextField;         //密码(设置密码)
@property (nonatomic, strong) YWKCustomTextField *ensurePwdTextField;   //确认密码

//
@property (nonatomic, strong) YWKDrawLineView * phoneSpareLine;
@property (nonatomic, strong) YWKDrawLineView * identySpareLine;
@property (nonatomic, strong) YWKDrawLineView * pwdSpareLine;
@property (nonatomic, strong) YWKDrawLineView * ensurePwdSpareLine;

@property (nonatomic, strong) UILabel * phoneNumberLable;
@property (nonatomic, strong) UILabel * identifyingCodeLable;
@property (nonatomic, strong) UILabel * pwdLable;
@property (nonatomic, strong) UILabel * ensurePwdLabel;
@property (nonatomic, strong) UITextView * userAgreementTV;
@property (nonatomic, strong) UIButton * identifyingBtn;                 //获取验证码
@property (nonatomic, strong) UIButton * rigterBtn;                      //确定or注册
@property (nonatomic, strong) UILabel * countDownTimer;
@property (nonatomic, assign) CGSize kbSize;
//@property (nonatomic, strong) ICTUserEntity *user;

@property (nonatomic, strong)UIAlertView * identifyActionAlert;
@property (nonatomic, strong)UIAlertView * registActionAlert;

@property (nonatomic, strong)UIView * serviceTerms;

@property (nonatomic, strong)NSString * phoneNumber;

@end

@implementation YWKRegisterVC

- (id)init
{
    self = [super init];
    if (self) {
//        _isAccoutChecking = NO;
    }
    return self;
}
- (void)loadView
{
    [super loadView];
    
    if (self.pwdType == ICTFromLoginRegistVC)
    {
        self.title = @"注册";
    }
    else
    {
        if (self.pwdType == ICTFromLoginFindBackVC)
        {
            self.title = @"找回密码";
        }
        else
        {
            self.title = @"修改密码";
        }
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    //导航返回按钮
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    leftBtn.frame = CGRectMake(0, 0, 22, 22);
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"backArrow"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBtnItem = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftBtnItem;
    
    self.navigationItem.rightBarButtonItem = nil;
    [self.view addSubview:self.rigisterScrollView];
    [self.rigisterScrollView addSubview:self.scrollContentView];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    // 约束
    [YWKHelper addConstraintInFirstItem:self.rigisterScrollView
                           toSecondItem:self.view
                               topSpace:0 leftSpace:0 rightSpace:0 bottomSpace:0 superView:nil];
    
    // content
    [YWKHelper addConstraintInFirstItem:self.scrollContentView
                           toSecondItem:self.rigisterScrollView
                               topSpace:10 leftSpace:0 rightSpace:0 bottomSpace:0 superView:nil];
    
    [YWKHelper onlyAddVerticalCentreYConstraintInFirstItem:self.scrollContentView
                                              toSecondItem:self.rigisterScrollView];
    [YWKHelper onlyAddVerticalCentreXConstraintInFirstItem:self.scrollContentView
                                              toSecondItem:self.rigisterScrollView];
    
    
    // lab
    [YWKHelper addConstraintInFirstItem:self.phoneNumberLable
                           toSecondItem:self.scrollContentView
                               topSpace:0 leftSpace:10 height:42 width:labWidth superView:nil];
    [YWKHelper addConstraintInFirstItem:self.identifyingCodeLable
                           toSecondItem:self.scrollContentView
                               topSpace:42 + 1 leftSpace:10 height:42 width:labWidth superView:nil];
    [YWKHelper addConstraintInFirstItem:self.pwdLable
                           toSecondItem:self.scrollContentView
                               topSpace:42 * 2 + 1 leftSpace:10 height:42 width:labWidth superView:nil];
    [YWKHelper addConstraintInFirstItem:self.ensurePwdLabel
                           toSecondItem:self.scrollContentView
                               topSpace:42 * 3 + 1 leftSpace:10 height:42 width:labWidth superView:nil];
    
    // line
    [YWKHelper addConstraintInFirstItem:self.phoneSpareLine
                           toSecondItem:self.scrollContentView
                               topSpace:42 leftSpace:10 rightSpace:-10 height:1 superView:nil];
    [YWKHelper addConstraintInFirstItem:self.identySpareLine
                           toSecondItem:self.scrollContentView
                               topSpace:42 * 2 leftSpace:10 rightSpace:-10 height:1 superView:nil];
    [YWKHelper addConstraintInFirstItem:self.pwdSpareLine
                           toSecondItem:self.scrollContentView
                               topSpace:42 * 3 leftSpace:10 rightSpace:-10 height:1 superView:nil];
    [YWKHelper addConstraintInFirstItem:self.ensurePwdSpareLine
                           toSecondItem:self.scrollContentView
                               topSpace:42 * 4 leftSpace:0 rightSpace:0 height:1 superView:nil];
    
    //
    [YWKHelper addConstraintInFirstItem:self.userAgreementTV
                           toSecondItem:self.scrollContentView
                               topSpace:42 * 4 + 1 leftSpace:0 rightSpace:0 height:42 superView:nil];
    
    // textField
    [YWKHelper addConstraintInFirstItem:self.phoneNumberTextField
                           toSecondItem:self.scrollContentView
                               topSpace:0 leftSpace:10 + labWidth rightSpace:-10 height:42 superView:nil];
    [YWKHelper addConstraintInFirstItem:self.identifyingCodeTF
                           toSecondItem:self.scrollContentView
                               topSpace:42 + 1 leftSpace:10 + labWidth rightSpace:-10 height:42 superView:nil];
    [YWKHelper addConstraintInFirstItem:self.pwdTextField
                           toSecondItem:self.scrollContentView
                               topSpace:42 * 2 + 1 leftSpace:10 + labWidth rightSpace:-10 height:42 superView:nil];
    [YWKHelper addConstraintInFirstItem:self.ensurePwdTextField
                           toSecondItem:self.scrollContentView
                               topSpace:42 * 3 + 1 leftSpace:10 + labWidth rightSpace:-10 height:42 superView:nil];
    
    // 验证码
    [YWKHelper addConstraintInFirstItem:self.identifyingBtn
                           toSecondItem:self.scrollContentView
                               topSpace:42 + 1 + (42 - 34) / 2 rightSpace:-10 height:34 width:100 superView:nil];
    
    [YWKHelper addConstraintInFirstItem:self.countDownTimer
                           toSecondItem:self.scrollContentView
                               topSpace:42 + 1 + (42 - 34) / 2 rightSpace:-10 height:34 width:100 superView:nil];
    
    // 注册 or 确定
    [YWKHelper addConstraintInFirstItem:self.rigterBtn
                           toSecondItem:self.scrollContentView
                            bottomSpace:-10 leftSpace:10 rightSpace:-10 height:40 superView:nil];
    
    

}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
//    [[NSNotificationCenter defaultCenter] addObserver:self
//                                             selector:@selector(keyboardWillShowInRigister:)
//                                                 name:UIKeyboardWillShowNotification
//                                               object:nil];
//    
//    [[NSNotificationCenter defaultCenter] addObserver:self
//                                             selector:@selector(keyboardWillBeHiddenInRigister:)
//                                                 name:UIKeyboardWillHideNotification
//                                               object:nil];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIKeyboardWillShowNotification
                                                  object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIKeyboardWillHideNotification
                                                  object:nil];
    
}

#pragma mark - UITextField delegate

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField == self.phoneNumberTextField)
    {
        
    }
    else if (textField == self.pwdTextField)
    {
        
    }
    else if (textField == self.ensurePwdTextField)
    {
        
    }
    else if (textField == self.identifyingCodeTF)
    {
        
    }
}

//隐藏键盘
- (void)hidenKeyboard
{
    [self.identifyingCodeTF resignFirstResponder];
    [self.pwdTextField resignFirstResponder];
    [self.phoneNumberTextField resignFirstResponder];
    [self.ensurePwdTextField resignFirstResponder];
}

//点击键盘上的return按钮响应的方法
- (void)nextOnKeyboard:(UITextField *)textfield
{
    if (textfield == self.phoneNumberTextField) {
        [self.identifyingCodeTF becomeFirstResponder];
    }
    else if (textfield == self.identifyingCodeTF)
    {
        [self.pwdTextField becomeFirstResponder];
    }
    else if (textfield == self.pwdTextField)
    {
        [self.ensurePwdTextField becomeFirstResponder];
    }
    else if (textfield == self.ensurePwdTextField)
    {
        [self hidenKeyboard];
    }
}

- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string
{
    if (textField.tag == kPhoneNumberTag)
    {
        
        if (self.pwdType == ICTFromLoginRegistVC)
        {
            //增加
            if (string.length > 0)
            {
                if ((textField.text.length == 3)
                    ||(textField.text.length == 8))
                {
                    textField.text = [NSString stringWithFormat:@"%@ ", textField.text];
                    range.location += 1;
                }
            }//删除
            else
            {
                if ((textField.text.length == 5)
                    ||(textField.text.length == 10))
                {
                    textField.text = [textField.text substringToIndex:range.location];
                    
                }
            }
        }
    }
    return YES;
}


#pragma mark - UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
    if ([[URL scheme] isEqualToString:@"YWK"]) {
        return NO;
    }
    return YES;
}

#pragma mark - Button Actions

- (void)back
{
    if (self.isPresented) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)cancelAction
{
    _isAccoutChecking = YES;
    [self.navigationController popViewControllerAnimated:YES];
}

/**
 *  @brief 在注册成功之后初始化新用户
 *  @author Jovi
 */
- (void)configNewUser:(NSString *)name password:(NSString *)pwd phoneNumber:(NSString *)phoneNumber
{
    //本地添加用户
//    ICTUserEntity * user = [[ICTUserEntity alloc] initWithName:[name lowercaseString]];
//    user.role = ICTUserRoleLogining;
//    user.selectedMonitorID = kICTMonitorTemplateIDMax;
//    user.phone = phoneNumber;
//    user.password = pwd;
//    [[ICTSettingManager sharedSettingManager] setUsername:user.name andPassword:pwd];
//    
//    ICTBathDao *bathDao = [ICTBathDao sharedBathDao];
//    [bathDao replaceUser:user withSuccess:nil failed:nil];
    
    //设置当前用户，以供后面几个页面使用 renyi
//    [ICTRuntimeInfo sharedRuntimeInfo].currentUser = user;
}

- (void)identifyingCodeClick
{
    
}

#pragma mark - 获取注册验证码
// 手机号
- (void)fetchIdentifyingCodeForReigister
{
    self.phoneNumber = [self.phoneNumberTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    //判断帐号是否存在
    //    [[ICTHttpClient sharedHttpClient]
    //     searchUserWithUsername:self.phoneNumber
    //     success:^(id content, NSError *error)
}

// 用户名
- (void)fetchIdentifyingCodeByUsernameReigister
{
    self.phoneNumber = [self.phoneNumberTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    
}


#pragma mark - 获取找回密码验证码
- (void) fetchIdentifyingCodeForForgetPwd
{
    self.phoneNumber = [self.phoneNumberTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    //避免输入中文崩溃
    
    //找判断帐号是否存在
}

#pragma mark - 获取改变密码验证码
- (void)fetchIdentifyingCodeForChangePwd
{
   
}

#pragma mark - 验证码与计时器
- (void)sendToServer:(ICTpwdAboutType)type
{
    //1.发送给server请求
    //2.更改btn状态
}

- (void)countDown
{
    _count = 0;
    _timer = [[NSTimer alloc]init];
    [self.identifyingBtn setEnabled:NO];
    [self.identifyingBtn setTitle:nil
                         forState:UIControlStateNormal];
    //    [self.identifyingBtn addSubview:self.countDownTimer];
    self.identifyingBtn.hidden = YES;
    self.countDownTimer.hidden = NO;
    [self getIFCFromeServer];
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countTime) userInfo:nil repeats:YES];
}

/**
 *	@brief	获取验证码
 */
- (void)getIFCFromeServer
{
    [self hidenKeyboard];
    
    if (self.pwdType == ICTFromLoginRegistVC)
    {
    }
}

- (void)showIdentifCode
{
    _identifyActionAlert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"温馨提示", nil)
                                                      message:NSLocalizedString(@"手机验证码已发送请查看手机短信", nil)
                                                     delegate:nil
                                            cancelButtonTitle:NSLocalizedString(@"确定", "确定")
                                            otherButtonTitles:nil];
    [_identifyActionAlert show];
    [NSTimer scheduledTimerWithTimeInterval:5
                                     target:self
                                   selector:@selector(alertViewDismissAction)
                                   userInfo:nil
                                    repeats:NO];
    [self.identifyingCodeTF resignFirstResponder];
}

//5s弹框消失
- (void)alertViewDismissAction
{
    [_identifyActionAlert dismissWithClickedButtonIndex:0 animated:YES];
    [_registActionAlert dismissWithClickedButtonIndex:0 animated:YES];
}

- (void)countTime
{
    _count++;
    _identifyingBtn.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.2];
    [self.countDownTimer setText:[NSString stringWithFormat:NSLocalizedString(@"%d%@",@"倒数"),
                                  (zCodeCountTime - _count),
                                  NSLocalizedString(@"秒后重新获取", nil)]];
    if (zCodeCountTime == _count)
    {
        self.countDownTimer.text = NSLocalizedString(@"60秒后重新获取", nil);
    }
    if (_count >= zCodeCountTime)
    {
        [_timer invalidate];
        [self reSendIdentifyCode];
    }
}

- (void)reSendIdentifyCode
{
//    [self.identifyingBtn setEnabled:YES];
    self.identifyingBtn.userInteractionEnabled = YES;
    //    [self.countDownTimer removeFromSuperview];
    self.countDownTimer.hidden = YES;
    self.identifyingBtn.hidden = NO;
    _identifyingBtn.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.3];
    self.identifyingBtn.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:16];
    [self.identifyingBtn setTitle:NSLocalizedString(@"重新获取",@"重发")
                         forState:UIControlStateNormal];
}
- (void)registerClick
{
    [self hidenKeyboard];
    //判断网络
    //输入有效性判断
}

#pragma mark - 验证 验证码
- (void)verificateIdentifyCodeForRegisterClick
{
}

- (void)verificateIdentifyCodeForChangePwdAgreeClick
{
}

#pragma mark - 找回密码
- (void)agreeAction
{
    
}

#pragma mark  - 注册

// 手机号/用户名
- (void)registAction
{
    
}

- (void)actionLoginWithUsername:(NSString *)userName password:(NSString *)password
{
    
}

- (BOOL)isInputLegal
{
    return NO;
}

//#pragma mark - other actions - keyboard
//- (void)keyboardWillShowInRigister:(NSNotification *)note
//{
//    //禁完成按钮
//    _rigterBtn.userInteractionEnabled = NO;
//    
//    NSDictionary* info = [note userInfo];
//    
//    self.kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
//    
//    NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
//    if (![duration doubleValue])
//    {
//        duration = [NSNumber numberWithFloat:0.3];
//    }
//    
//    CGFloat offset = [self calculateOffsetY];
//    [self.rigisterScrollView setContentOffset:CGPointMake(0, offset)
//                                     animated:YES];
//}
//
//- (void)keyboardWillBeHiddenInRigister:(NSNotification *)note
//{
//    //解完成按钮
//    _rigterBtn.userInteractionEnabled = YES;
//    
//    NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
//    [UIView animateWithDuration:[duration doubleValue]
//                     animations:^{
//                         [self.rigisterScrollView setContentOffset:CGPointMake(0, 0) animated:NO];
//                     }];
//}

/**
 *  计算偏移量
 *
 *  @return Y偏移量
 */
- (CGFloat)calculateOffsetY
{
    CGFloat offsetY = 0.0;
    if (self.phoneNumberTextField.isFirstResponder)
    {
        offsetY = 0.0;
    }
    else if (self.identifyingCodeTF.isFirstResponder)
    {
        offsetY = self.kbSize.height + 42 * 2 + 1 - self.scrollContentView.frame.size.height;
        if (offsetY > 0) {
            offsetY = offsetY;
        }
        else{
            offsetY = 0;
        }
    }
    else if (self.pwdTextField.isFirstResponder)
    {
        offsetY = self.kbSize.height + 42 * 3 + 1 - self.scrollContentView.frame.size.height;
        if (offsetY > 0) {
            offsetY = offsetY;
        }
        else{
            offsetY = 0;
        }
    }
    else if (self.ensurePwdTextField)
    {
        offsetY = self.kbSize.height + 42 * 4 + 1 - self.scrollContentView.frame.size.height;
        if (offsetY > 0) {
            offsetY = offsetY;
        }
        else{
            offsetY = 0;
        }
    }
    return offsetY;
}
#pragma mark - 用户协议
- (UIView *)serviceTerms
{
    if (!_serviceTerms)
    {
//        _serviceTerms = [[UIView alloc] initWithFrame:CGRectMake(18,
//                                                                 12,
//                                                                 self.view.frameWidth - 36,
//                                                                 self.view.frameHeight-IOS9VIEWFRAMEORIGIN-32)];
//        _serviceTerms.backgroundColor = [UIColor colorWithHex:0xb4c8d6 alpha:1.0];
//        
//        ICTWebView *webView = [[ICTWebView alloc] initWithFrame:CGRectMake(0,
//                                                                           0,
//                                                                           _serviceTerms.frameWidth,
//                                                                           _serviceTerms.frameHeight - 44)];
//        
//        webView.opaque = NO;
//        webView.backgroundColor = [UIColor clearColor];
//        
//        NSString *resourcePath = [ [NSBundle mainBundle] resourcePath];
//        
//        NSString *filePath  = [resourcePath stringByAppendingPathComponent:
//                               [NSString stringWithFormat:@"protocol_%@.html",[NSString currentLanguageStr]]];
//        NSString *htmlstring =[[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
//        [webView loadHTMLString:htmlstring  baseURL:[NSURL fileURLWithPath: [[NSBundle mainBundle]  bundlePath]]];
//        [_serviceTerms addSubview:webView];
//        
//        ICTDrawLine *hLine = [[ICTDrawLine alloc]initWithFrame:CGRectMake(0,
//                                                                          webView.frameHeight,
//                                                                          _serviceTerms.frameWidth,
//                                                                          1.0)];
//        [hLine LineWidth:_serviceTerms.frameWidth andHeight:1.0];
//        hLine.backgroundColor = [UIColor grayColor];
//        [_serviceTerms addSubview:hLine];
//        
//        ICTDrawLine *vLine = [[ICTDrawLine alloc]initWithFrame:CGRectMake(_serviceTerms.frameWidth / 2.0,
//                                                                          CGRectGetMaxY(hLine.frame),
//                                                                          1.0,
//                                                                          44)];
//        [vLine LineWidth:1.0 andHeight:44.0];
//        vLine.backgroundColor = [UIColor grayColor];
//        [_serviceTerms addSubview:vLine];
//        
//        UIButton *disAgreeBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//        disAgreeBtn.frame = CGRectMake(5,
//                                       _serviceTerms.frameHeight - 40,
//                                       _serviceTerms.frameWidth / 2.0 - 10,
//                                       40);
//        disAgreeBtn.titleLabel.font = [UIFont fontWithName:kTextFontName size:16.0];
//        
//        [disAgreeBtn setTitle:NSLocalizedString(@"取消", @"取消") forState:UIControlStateNormal];
//        [disAgreeBtn setBackgroundColor:[UIColor colorWithHex:0xb4c8d6 alpha:1.0]];
//        [disAgreeBtn addTarget:self
//                        action:@selector(serviceTermsDisAgreeClick)
//              forControlEvents:UIControlEventTouchUpInside];
//        
//        [_serviceTerms addSubview:disAgreeBtn];
//        
//        UIButton *agreeBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//        agreeBtn.frame = CGRectMake(_serviceTerms.frameWidth / 2.0 + 5,
//                                    _serviceTerms.frameHeight - 40,
//                                    _serviceTerms.frameWidth / 2.0 - 10,
//                                    40);
//        agreeBtn.titleLabel.font = [UIFont fontWithName:kTextFontName size:16.0];
//        
//        [agreeBtn setTitle:NSLocalizedString(@"同意", @"同意") forState:UIControlStateNormal];
//        [agreeBtn setBackgroundColor:[UIColor colorWithHex:0xb4c8d6 alpha:1.0]];
//        [agreeBtn addTarget:self
//                     action:@selector(serviceTermsAgreeClick)
//           forControlEvents:UIControlEventTouchUpInside];
//        
//        _serviceTerms.layer.masksToBounds = YES;
//        [_serviceTerms.layer setBorderWidth:1.0f];
//        [_serviceTerms.layer setBorderColor:[UIColor clearColor].CGColor];
//        _serviceTerms.layer.cornerRadius = 4.0f;
//        [_serviceTerms addSubview:agreeBtn];
    }
    return _serviceTerms;
}

- (void)serviceTermsDisAgreeClick
{
    [self.serviceTerms removeFromSuperview];
    //    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)serviceTermsAgreeClick
{
//    [UIView animateWithDuration:0.3f
//                          delay:0
//                        options:UIViewAnimationOptionCurveEaseIn
//                     animations:^
//     {
//         self.serviceTerms.alpha = 0.1f;
//         self.serviceTerms.frameY = UI_SCREEN_HEIGHT;
//     }
//                     completion:^(BOOL finished)
//     {
//         [self.serviceTerms removeFromSuperview];
//     }];
//    [self registAction];
}

#pragma mark - UI/Getter
- (UIScrollView *)rigisterScrollView
{
    if (!_rigisterScrollView)
    {
        _rigisterScrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
        _rigisterScrollView.backgroundColor = [UIColor clearColor];
        _rigisterScrollView.delegate = self;
        _rigisterScrollView.showsVerticalScrollIndicator = NO;
        _rigisterScrollView.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _rigisterScrollView;
}


- (UIView *)scrollContentView
{
    if (!_scrollContentView) {
        _scrollContentView = [[UIView alloc] initWithFrame:CGRectZero];
        _scrollContentView.backgroundColor = [UIColor clearColor];
        _scrollContentView.translatesAutoresizingMaskIntoConstraints = NO;
        
        //注册页面有C＋图
        if (self.pwdType == ICTFromLoginRegistVC)
        {
            //            [_rigisterScrollView addSubview:self.iCTLogoView];
        }
        
        //各个输入项
        [_scrollContentView addSubview:self.phoneNumberTextField];
        [_scrollContentView addSubview:self.identifyingCodeTF];
        [_scrollContentView addSubview:self.pwdTextField];
        [_scrollContentView addSubview:self.ensurePwdTextField];
        
        //各输入项的label
        [_scrollContentView addSubview:self.phoneNumberLable];
        [_scrollContentView addSubview:self.identifyingCodeLable];
        [_scrollContentView addSubview:self.pwdLable];
        [_scrollContentView addSubview:self.ensurePwdLabel];
        
        
        if (self.pwdType == ICTFromSetVC) {
            self.phoneSpareLine.hidden = YES;
        }
        [_scrollContentView addSubview:self.phoneSpareLine];
        [_scrollContentView addSubview:self.identySpareLine];
        [_scrollContentView addSubview:self.pwdSpareLine];
        [_scrollContentView addSubview:self.ensurePwdSpareLine];
        
        //
        [_scrollContentView addSubview:self.userAgreementTV];
        
        //获取验证码
        [_scrollContentView addSubview:self.identifyingBtn];
        [_scrollContentView addSubview:self.countDownTimer];
        
        //添加手势
        UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc]
                                           initWithTarget:self
                                           action:@selector(hidenKeyboard)];
        gesture.numberOfTapsRequired = 1;
        [_scrollContentView addGestureRecognizer:gesture];
        
        //创建帐号按钮
        [_scrollContentView addSubview:self.rigterBtn];
    }
    return _scrollContentView;
}

- (YWKDrawLineView *)phoneSpareLine
{
    if (!_phoneSpareLine) {
        _phoneSpareLine = [[YWKDrawLineView alloc] initWithFrame:CGRectZero];
        _phoneSpareLine.translatesAutoresizingMaskIntoConstraints = NO;
        _phoneSpareLine.backgroundColor = [UIColor lightGrayColor];
    }
    return _phoneSpareLine;
}

- (YWKDrawLineView *)identySpareLine
{
    if (!_identySpareLine) {
        _identySpareLine = [[YWKDrawLineView alloc] initWithFrame:CGRectZero];
        _identySpareLine.translatesAutoresizingMaskIntoConstraints = NO;
        _identySpareLine.backgroundColor = [UIColor lightGrayColor];
    }
    return _identySpareLine;
}

- (YWKDrawLineView *)pwdSpareLine
{
    if (!_pwdSpareLine) {
        _pwdSpareLine = [[YWKDrawLineView alloc] initWithFrame:CGRectZero];
        _pwdSpareLine.translatesAutoresizingMaskIntoConstraints = NO;
        _pwdSpareLine.backgroundColor = [UIColor lightGrayColor];
    }
    return _pwdSpareLine;
}

- (YWKDrawLineView *)ensurePwdSpareLine
{
    if (!_ensurePwdSpareLine) {
        _ensurePwdSpareLine = [[YWKDrawLineView alloc] initWithFrame:CGRectZero];
        _ensurePwdSpareLine.translatesAutoresizingMaskIntoConstraints = NO;
        _ensurePwdSpareLine.backgroundColor = [UIColor lightGrayColor];
    }
    return _ensurePwdSpareLine;
}


//iCareTech Logo View
- (UIImageView *)iCTLogoView
{
    if (!_iCTLogoView) {
        UIImage *logoImg = [UIImage imageNamed:@"iCTLogo"];
        _iCTLogoView = [[UIImageView alloc]initWithFrame:CGRectMake(119,
                                                                    22,
                                                                    logoImg.size.width,
                                                                    logoImg.size.height)];
        _iCTLogoView.image = logoImg;
    }
    
    return _iCTLogoView;
}

//手机号输入
- (YWKCustomTextField *)phoneNumberTextField
{
    if (!_phoneNumberTextField) {
        _phoneNumberTextField = [[YWKCustomTextField alloc]initWithFrame:CGRectZero];
        _phoneNumberTextField.translatesAutoresizingMaskIntoConstraints = NO;
        _phoneNumberTextField.delegate = self;
        _phoneNumberTextField.borderStyle = UITextBorderStyleNone;
        _phoneNumberTextField.backgroundColor = [UIColor clearColor];
        _phoneNumberTextField.textColor = [UIColor blackColor];
        
        if (self.pwdType == ICTFromLoginRegistVC)
        {
            _phoneNumberTextField.placeholder = NSLocalizedString(@"请输入手机号码",nil);
        }
        else
        {
            _phoneNumberTextField.placeholder = NSLocalizedString(@"请输入手机号/帐号",nil);
        }
        _phoneNumberTextField.placeHolderColor = kPlaceHolderColor;
        _phoneNumberTextField.placeHolderSize = 16.0f;
        _phoneNumberTextField.clearButtonMode= UITextFieldViewModeWhileEditing;
        _phoneNumberTextField.returnKeyType = UIReturnKeyNext;
        [_phoneNumberTextField addTarget:self
                                  action:@selector(nextOnKeyboard:)
                        forControlEvents:UIControlEventEditingDidEndOnExit];
        _phoneNumberTextField.tag = kPhoneNumberTag;
        
    }
    return _phoneNumberTextField;
}

//验证码输入
- (YWKCustomTextField *)identifyingCodeTF
{
    
    if (!_identifyingCodeTF) {
        _identifyingCodeTF = [[YWKCustomTextField alloc]initWithFrame:CGRectZero];
        _identifyingCodeTF.translatesAutoresizingMaskIntoConstraints = NO;
        _identifyingCodeTF.delegate = self;
        _identifyingCodeTF.borderStyle = UITextBorderStyleNone;
        _identifyingCodeTF.backgroundColor = [UIColor clearColor];
        _identifyingCodeTF.textColor = [UIColor blackColor];
        _identifyingCodeTF.placeholder = NSLocalizedString(@"请输入验证码",nil);
        _identifyingCodeTF.placeHolderColor = kPlaceHolderColor;
        _identifyingCodeTF.placeHolderSize = 16.0f;
        _identifyingCodeTF.clearButtonMode= UITextFieldViewModeWhileEditing;
        _identifyingCodeTF.clearsOnBeginEditing = NO;
        _identifyingCodeTF.keyboardType = UIKeyboardTypeNumberPad;
        [_identifyingCodeTF addTarget:self
                               action:@selector(nextOnKeyboard:)
                     forControlEvents:UIControlEventEditingDidEndOnExit];
    }
    
    return _identifyingCodeTF;
}

//设置密码输入
- (YWKCustomTextField *)pwdTextField
{
    if (!_pwdTextField) {
        _pwdTextField = [[YWKCustomTextField alloc]initWithFrame:CGRectZero];
        _pwdTextField.translatesAutoresizingMaskIntoConstraints = NO;
        _pwdTextField.delegate = self;
        _pwdTextField.borderStyle = UITextBorderStyleNone;
        _pwdTextField.backgroundColor = [UIColor clearColor];
        _pwdTextField.textColor = [UIColor blackColor];
        _pwdTextField.placeholder = NSLocalizedString(@"6-15位数字或字母",nil);
        _pwdTextField.placeHolderColor = kPlaceHolderColor;
        _pwdTextField.placeHolderSize = 16.0f;
        _pwdTextField.clearButtonMode= UITextFieldViewModeWhileEditing;
        _pwdTextField.secureTextEntry = YES;
        _pwdTextField.returnKeyType = UIReturnKeyNext;
        [_pwdTextField addTarget:self
                          action:@selector(nextOnKeyboard:)
                forControlEvents:UIControlEventEditingDidEndOnExit];
    }
    return _pwdTextField;
}

//确认密码输入
- (YWKCustomTextField *)ensurePwdTextField
{
    if (!_ensurePwdTextField) {
        _ensurePwdTextField = [[YWKCustomTextField alloc]initWithFrame:CGRectZero];
        _ensurePwdTextField.translatesAutoresizingMaskIntoConstraints = NO;
        _ensurePwdTextField.delegate = self;
        _ensurePwdTextField.borderStyle = UITextBorderStyleNone;
        _ensurePwdTextField.backgroundColor = [UIColor clearColor];
        _ensurePwdTextField.textColor = [UIColor blackColor];
        _ensurePwdTextField.placeholder = NSLocalizedString(@"请再次输入密码",nil);
        _ensurePwdTextField.placeHolderColor = kPlaceHolderColor;
        _ensurePwdTextField.placeHolderSize = 16.0f;
        _ensurePwdTextField.clearButtonMode= UITextFieldViewModeWhileEditing;
        _ensurePwdTextField.secureTextEntry = YES;
        _ensurePwdTextField.returnKeyType = UIReturnKeyNext;
        [_ensurePwdTextField addTarget:self
                                action:@selector(nextOnKeyboard:)
                      forControlEvents:UIControlEventEditingDidEndOnExit];
    }
    return _ensurePwdTextField;
}



//手机号、验证码、密码、确认密码的label

- (UILabel *)phoneNumberLable
{
    if (!_phoneNumberLable) {
        _phoneNumberLable = [[UILabel alloc]initWithFrame:CGRectZero];
        _phoneNumberLable.translatesAutoresizingMaskIntoConstraints = NO;
        _phoneNumberLable.textAlignment = NSTextAlignmentLeft;
        _phoneNumberLable.font = [UIFont fontWithName:@"HelveticaNeue" size:16.0];
        _phoneNumberLable.textColor = [UIColor blackColor];
        
        if (self.pwdType == ICTFromLoginRegistVC) {
            _phoneNumberLable.text = NSLocalizedString(@"手机号码", @"手机号码");
        }
        else if (self.pwdType == ICTFromLoginFindBackVC)
        {
            _phoneNumberLable.text = NSLocalizedString(@"帐号", @"帐号");
        }
    }
    
    return _phoneNumberLable;
}

- (UILabel *)identifyingCodeLable
{
    if (!_identifyingCodeLable) {
        _identifyingCodeLable = [[UILabel alloc]initWithFrame:CGRectZero];
        _identifyingCodeLable.translatesAutoresizingMaskIntoConstraints = NO;
        _identifyingCodeLable.textAlignment = NSTextAlignmentLeft;
        _identifyingCodeLable.font = [UIFont fontWithName:@"HelveticaNeue" size:16.0];
        _identifyingCodeLable.textColor = [UIColor blackColor];
        _identifyingCodeLable.text = NSLocalizedString(@"验证码", @"验证码");
    }
    
    return _identifyingCodeLable;
}

- (UILabel *)pwdLable
{
    if (!_pwdLable) {
        _pwdLable = [[UILabel alloc]initWithFrame:CGRectZero];
        _pwdLable.translatesAutoresizingMaskIntoConstraints = NO;
        _pwdLable.textAlignment = NSTextAlignmentLeft;
        _pwdLable.font = [UIFont fontWithName:@"HelveticaNeue" size:16.0];
        _pwdLable.textColor = [UIColor blackColor];
        if(self.pwdType == ICTFromLoginRegistVC)
        {
            _pwdLable.text = NSLocalizedString(@"设置密码", @"设置密码");
        }
        else
        {
            _pwdLable.text = NSLocalizedString(@"设置密码", @"设置密码");
        }
    }
    
    return _pwdLable;
}

- (UILabel *)ensurePwdLabel
{
    if (!_ensurePwdLabel) {
        _ensurePwdLabel = [[UILabel alloc]initWithFrame:CGRectZero];
        _ensurePwdLabel.translatesAutoresizingMaskIntoConstraints = NO;
        _ensurePwdLabel.textAlignment = NSTextAlignmentLeft;
        _ensurePwdLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:16.0];
        _ensurePwdLabel.textColor = [UIColor blackColor];
        _ensurePwdLabel.text = NSLocalizedString(@"确认密码", @"确认密码");
    }
    
    return _ensurePwdLabel;
}

- (UITextView *)userAgreementTV
{
    if (!_userAgreementTV) {
        _userAgreementTV = [[UITextView alloc] initWithFrame:CGRectZero];
        _userAgreementTV.translatesAutoresizingMaskIntoConstraints = NO;
        _userAgreementTV.backgroundColor = [UIColor clearColor];
        _userAgreementTV.font = [UIFont fontWithName:@"HelveticaNeue" size:14];
        _userAgreementTV.textColor = [UIColor blackColor];
        _userAgreementTV.editable = NO;
        _userAgreementTV.selectable = YES;
        _userAgreementTV.delegate = self;
        _userAgreementTV.hidden = YES;
        
        NSMutableAttributedString * mAttributeStr = [[NSMutableAttributedString alloc]
                                                     initWithString:agreeServiceTermsText];
        NSMutableParagraphStyle * paragrathStytle = [[NSMutableParagraphStyle alloc] init];
        
        paragrathStytle.alignment = NSTextAlignmentCenter;
        [mAttributeStr addAttribute:NSParagraphStyleAttributeName
                              value:paragrathStytle.copy
                              range:NSMakeRange(0, agreeServiceTermsText.length)];
        
        [mAttributeStr addAttribute:NSForegroundColorAttributeName
                              value:[UIColor blackColor]
                              range:NSMakeRange(0, agreeServiceTermsText.length - YWKServiceTerms.length)];
        [mAttributeStr addAttribute:NSFontAttributeName
                              value:[UIFont fontWithName:@"HelveticaNeue" size:14]
                              range:NSMakeRange(0, agreeServiceTermsText.length - YWKServiceTerms.length)];
        
        NSRange  termsRange = [agreeServiceTermsText rangeOfString:YWKServiceTerms];
        [mAttributeStr addAttribute:NSForegroundColorAttributeName
                              value:[UIColor blueColor] range:termsRange];
        [mAttributeStr addAttribute:NSFontAttributeName
                              value:[UIFont fontWithName:@"HelveticaNeue" size:14]
                              range:termsRange];
        [mAttributeStr addAttribute:NSLinkAttributeName
                              value:@"YWK://useragreement"
                              range:termsRange];
        
        _userAgreementTV.attributedText = mAttributeStr.copy;
        
        if (_pwdType == ICTFromLoginRegistVC) {
            _userAgreementTV.hidden = NO;
        }
    }
    return _userAgreementTV;
}


- (UIButton *)identifyingBtn
{
    
    if (!_identifyingBtn)
    {
        _identifyingBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        _identifyingBtn.frame = CGRectZero;
        _identifyingBtn.translatesAutoresizingMaskIntoConstraints = NO;
        [_identifyingBtn.titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:16.0]];
        [_identifyingBtn setTitle:NSLocalizedString(@"获取验证码",nil)
                         forState:UIControlStateNormal];
        _identifyingBtn.tintColor = [UIColor blackColor];
        
        _identifyingBtn.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.2];
        
        _identifyingBtn.layer.cornerRadius = 5;
        _identifyingBtn.layer.masksToBounds = YES;
        [_identifyingBtn addTarget:self
                            action:@selector(identifyingCodeClick)
                  forControlEvents:UIControlEventTouchUpInside];
    }
    return _identifyingBtn;
}

- (UIButton *)rigterBtn
{
    if (!_rigterBtn) {
        //创建帐号按钮
        _rigterBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        _rigterBtn.frame =CGRectZero;
        _rigterBtn.translatesAutoresizingMaskIntoConstraints = NO;
        [_rigterBtn.titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:16.0]];
        if (self.pwdType == ICTFromLoginRegistVC)
        {
            [_rigterBtn setTitle:NSLocalizedString(@"注册",@"注册")
                        forState:UIControlStateNormal];
            
        }
        else
        {
            [_rigterBtn setTitle:NSLocalizedString(@"确定",@"确定")
                        forState:UIControlStateNormal];
        }
        UIImage *normalBtnImg = [[UIImage imageNamed:@"normalBtn"]
                                 resizableImageWithCapInsets:UIEdgeInsetsMake(3, 3, 3, 3)];
        UIImage *highlightedImg = [[UIImage imageNamed:@"highligtedBtn"]
                                   resizableImageWithCapInsets:UIEdgeInsetsMake(3, 3, 3, 3)];
        [_rigterBtn setBackgroundImage:normalBtnImg forState:UIControlStateNormal];
        [_rigterBtn setBackgroundImage:highlightedImg forState:UIControlStateHighlighted];
        [_rigterBtn addTarget:self
                       action:@selector(registerClick)
             forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _rigterBtn;
}

- (UILabel *)countDownTimer
{
    if (!_countDownTimer)
    {
        _countDownTimer  = [[UILabel alloc] initWithFrame:CGRectZero];
        _countDownTimer.translatesAutoresizingMaskIntoConstraints = NO;
        _countDownTimer.font = [UIFont fontWithName:@"HelveticaNeue" size:14.f];
        _countDownTimer.text =  NSLocalizedString(@"60秒后重新获取",nil);
        _countDownTimer.textColor = [UIColor blackColor];
        _countDownTimer.hidden = YES;
        _countDownTimer.textAlignment = NSTextAlignmentCenter;
    }
    return _countDownTimer;
}


@end
